
import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * The game's main menu.
 * @author Akshay
 *
 */
public class MainMenuState extends BasicGameState implements IButtonListener {	

	int stateID = 0;
	
	private ArrayList<LabeledButton> buttonList;
	
	private boolean enterGameFlag = false;
	private boolean enterSettingsFlag = false;
	private boolean exitFlag = false;
	
	
	/**
	 * Constructor for MainMenuState
	 * @param ID the ID for this state as defined in the Game class
	 */
	public MainMenuState(int ID){
		stateID = ID;
	}

	@Override
	public void init(GameContainer container, StateBasedGame game)throws SlickException {
		//This is the FIRST thing the game does!
		Resources.loadResources();
		
		//initialize menu buttons		
		int buttonX = (container.getWidth()-Resources.image_mainmenu_button.getWidth())/2;	 	
		buttonList = new ArrayList<LabeledButton>();
		buttonList.add(new LabeledButton(Resources.image_mainmenu_button, Resources.image_mainmenu_button_hover, Resources.image_mainmenu_button_down, buttonX, 200, "Play", this)); // PLAY button
		buttonList.add(new LabeledButton(Resources.image_mainmenu_button, Resources.image_mainmenu_button_hover, Resources.image_mainmenu_button_down, buttonX, 300, "Settings", this)); // SETTINGS button
		buttonList.add(new LabeledButton(Resources.image_mainmenu_button, Resources.image_mainmenu_button_hover, Resources.image_mainmenu_button_down, buttonX, 400, "Exit", this)); // EXIT button

	}
	
	@Override
	public void enter(GameContainer container, StateBasedGame game) throws SlickException{	
		enterGameFlag = false;
		enterSettingsFlag = false;
		exitFlag = false;
	}
	


	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)throws SlickException {
		
		//draw background
		Resources.image_mainmenu_background.draw(0,0);
		Resources.image_mainmenu_title.draw((container.getWidth()-Resources.image_mainmenu_title.getWidth())/2, 50);
		
		//draw menu buttons
		for(LabeledButton b:buttonList){
			b.render(container, game, g);
		}
				
	}

	@Override 
	
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		
		for(LabeledButton b:buttonList){
			b.update(container, game);
		}
		
		if(enterGameFlag){
			((Game)game).enterState(Game.STATE_GAMEPLAY);
		}
		
		if(enterSettingsFlag){
			((Game)game).enterState(Game.STATE_SETTINGS);
		}
		
		if(exitFlag){
			System.out.println("exit");
			((Game)game).exit();
		}
	}
	
	@Override
	public void buttonClicked(LabeledButton source) {
		if(source instanceof LabeledButton){  
			switch(((LabeledButton)source).getLabel()){
			case "Play":
				enterGameFlag = true;
				break;
			case "Settings":
				enterSettingsFlag = true;
				break;
			case "Exit":
				exitFlag = true;
				break;
			default:
				break;
			}
		}
	}

	@Override
	public int getID() {
		return stateID;
	}



}
